<?php

namespace app\index\controller;

use app\common\controller\Frontend;

class Index extends Frontend
{

    protected $noNeedLogin = '*';
    protected $noNeedRight = '*';
    protected $layout = '';

    public function index()
    {
        return $this->view->fetch();
    }

    //模拟推送
    public function set_order(){

        //乐刷不对测试环境的交易订单进行清分、入账和出款，请直接在生产环境进行调试。
        $url = "http://localhost:9005/api/abutment/set_order";
        $public = array(
            "version" => "1.0",
        );

        $data = array(
            "merchant_id" => '6069817388',
            "amount" => '1500',
            "sub_openid" => 'os1GovgPqdbAJLjRTja-S8QtvA9M',
            "remark" => "分账",
        );

        $sign  = base64_encode(strtolower(md5("lepos".config('leshua.key').json_encode($data))));
        $public["sign"] = $sign;
        $public["data"] = json_encode($data);

        $res = $this->http_curl($url, 'post', $public);
        halt($res);
        $res = json_decode($res,true);
        if (isset($res['respCode'])) {
            if ($res['respCode'] == '000000') {
                $resdata = $res['data'];
                if ($resdata['retcode'] != '0') {
                    return ['status' => false, 'error_msg' => $resdata['retmsg'], 'error_code' => $resdata['retcode']];
                } else {

                    return ['status' => true, 'error_msg' => $resdata['retmsg'], 'error_code' => $resdata['retcode']];
                }
            }else{
                return ['status' => false, 'error_msg' => $res['respMsg'], 'error_code' => $res['respCode']];
            }
        } else {
            return ['status' => false, 'error_msg' => '分账异常', 'error_code' => -1];
        }
    }

    public function http_curl($url = "", $type = 'get', $arr = '',$head = '')
    {
        $ch = curl_init();
        //2.设置curl的参数
        curl_setopt($ch, CURLOPT_URL, $url); //设置我们的url
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); //curl_exec执行成功则返回执行结果
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
        if ($type == 'post') {
            curl_setopt($ch, CURLOPT_POST, 1);
            curl_setopt($ch, CURLOPT_POSTFIELDS, $arr); //将$arr发送给$url
        }
        //curl_setopt($ch,CURLINFO_CONTENT_TYPE,"application/json");
        $output = curl_exec($ch);
        curl_close($ch);
        return $output;
    }

    public function new_award(){
        $res = new_award(2000);
        halt($res);
    }

}
